package com.yk.i_wms.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.dto.SmsOutStockDocDTO;
import com.yk.i_wms.model.SmsOutStockDoc;
import com.yk.i_wms.model.SmsOutStockDocDetail;
import com.yk.i_wms.model.SmsOutStockDocRecord;
import com.yk.i_wms.service.ISmsOutStockDocDetailService;
import com.yk.i_wms.service.ISmsOutStockDocRecordService;
import com.yk.i_wms.service.ISmsOutStockDocService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 出库单
 *
 * @author hbq
 * @date 2021-04-07 14:20:31
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "出库单")
@RequestMapping("sms")
public class SmsOutStockDocController {
    @Autowired
    private ISmsOutStockDocService smsOutStockDocService;
    @Autowired
    private ISmsOutStockDocDetailService smsOutStockDocDetailService;
    @Autowired
    private ISmsOutStockDocRecordService smsOutStockDocRecordService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/smsOutStockDoc/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsOutStockDocService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }
    /**
     * 亮灯
     */
    @ApiOperation(value = "亮灯")
    @PostMapping("/smsOutStockDoc/lightUp")
    public Result lightUp(@RequestBody Map<String, Object> params) {
        return smsOutStockDocService.lightUp(params);
    }

    /**
     * 扫码备料
     */
    @ApiOperation(value = "扫码备料")
    @PostMapping("/smsOutStockDoc/scanSN")
    public Result scanSN(@RequestBody Map<String, Object> params) {
        return smsOutStockDocService.scanSN(params);
    }

    /**
     * 取消扫码出库
     */
    @ApiOperation(value = "取消扫码出库")
    @PostMapping("/smsOutStockDoc/scanCancel")
    public Result scanCancel(@RequestBody Map<String, Object> params) {
        return smsOutStockDocService.scanCancel(params);
    }
    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/smsOutStockDoc/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        SmsOutStockDoc model = smsOutStockDocService.getById(id);
        return Result.succeed(model, "查询成功");
    }
    /**
     * 根据单号查详情
     */
    @ApiOperation(value = "根据单号查详情")
    @PostMapping("/smsOutStockDoc/selAllByDocNo")
    public Result selAllByDocNo(@RequestBody Map<String, Object> params) {
        String docNo = MapUtil.getStr(params, "docNo");
        SmsOutStockDoc outStockDoc = smsOutStockDocService.getOne(new LambdaQueryWrapper<SmsOutStockDoc>()
                .eq(SmsOutStockDoc::getDocNo, docNo));
        Map<String, Object> resultMap = BeanUtil.beanToMap(outStockDoc);
        List<SmsOutStockDocDetail> outStockDocDetails = smsOutStockDocDetailService.list(new LambdaQueryWrapper<SmsOutStockDocDetail>().eq(SmsOutStockDocDetail::getDocNo, docNo));
        resultMap.put("outStockDocDetails",outStockDocDetails);
        return Result.succeed(resultMap, "查询成功");
    }
    /**
     * 根据详情id查sn记录
     */
    @ApiOperation(value = "根据详情id查sn记录")
    @PostMapping("/smsOutStockDoc/selRecordByDdId")
    public Result selRecordByDdid(@RequestBody Map<String, Object> params) {
        String ddId = MapUtil.getStr(params, "ddId");
        List<SmsOutStockDocRecord> records = smsOutStockDocRecordService.list(new LambdaQueryWrapper<SmsOutStockDocRecord>()
                .eq(SmsOutStockDocRecord::getDdId, ddId));
        return Result.succeed(records, "查询成功");
    }
    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/smsOutStockDoc/save")
    public Result save(@RequestBody SmsOutStockDoc smsOutStockDoc) {
        smsOutStockDocService.saveOrUpdate(smsOutStockDoc);
        return Result.succeed("保存成功");
    }
    /**
     * 新增or更新(包含详情)
     */
    @ApiOperation(value = "新增or更新(包含详情)")
    @PostMapping("/smsOutStockDoc/saveAll")
    public Result saveAll(@RequestBody SmsOutStockDocDTO smsOutStockDocDTO) {
        return smsOutStockDocService.saveAll(smsOutStockDocDTO);
    }
    /**
     * 关结
     */
    @ApiOperation(value = "关结")
    @PostMapping("/smsOutStockDoc/close")
    public Result close(@RequestBody SmsOutStockDoc smsOutStockDoc) {
        return smsOutStockDocService.close(smsOutStockDoc);
    }
    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/smsOutStockDoc/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<SmsOutStockDoc>> map) {
        List<SmsOutStockDoc> models = map.get("models");
        smsOutStockDocService.saveOrUpdateBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/smsOutStockDoc/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        return  smsOutStockDocService.delete(map);
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/smsOutStockDoc/leadIn")
    public  Result leadIn(MultipartFile excel) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<SmsOutStockDoc> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, SmsOutStockDoc.class);
            rowNum = list.size();
            if (rowNum > 0) {
                list.forEach(u -> {
                        smsOutStockDocService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/smsOutStockDoc/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<SmsOutStockDoc> smsOutStockDocList = ids==null||ids.isEmpty()? smsOutStockDocService.list():(List)smsOutStockDocService.listByIds(ids);
        //导出操作
        EasyPoiExcelUtil.exportExcel(smsOutStockDocList, "出库单导出", "出库单导出", SmsOutStockDoc.class, "smsOutStockDoc.xls", response);
    }
}
